<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="20" v-loading="loading">
      <el-col 
        :xs="24" :sm="12" :md="8" :lg="6" 
        v-for="item in productList" 
        :key="item.productId"
        class="mb20"
      >
        <el-card shadow="hover" class="product-card" :body-style="{ padding: '0px' }">
          <div class="image-placeholder">
            <el-icon size="50" color="#909399"><Goods /></el-icon>
          </div>
          
          <div class="card-body">
            <div class="product-title" :title="item.name">{{ item.name }}</div>
            <div class="product-desc" :title="item.description">{{ item.description || '暂无描述' }}</div>
            
            <div class="price-row">
              <span class="price">¥{{ item.price }}</span>
              <span class="stock">库存: {{ item.stockQuantity }}</span>
            </div>

            <div class="action-row">
              <div v-if="item.userCartQuantity > 0" class="added-status">
                <el-tag type="success" effect="plain">
                  <el-icon><ShoppingCartFull /></el-icon> 已加 {{ item.userCartQuantity }}
                </el-tag>
                <el-button 
                  type="primary" 
                  circle 
                  size="small" 
                  icon="Plus" 
                  class="ml-2"
                  @click="handleAdd(item)"
                ></el-button>
              </div>

              <el-button 
                v-else 
                type="primary" 
                plain 
                class="add-btn" 
                icon="ShoppingCart"
                @click="handleAdd(item)"
              >
                加入购物车
              </el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-empty v-if="total === 0 && !loading" description="暂无商品数据" />

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="AllProducts">
import { listAllProducts, addToCart } from "@/api/shopping/allProducts";
import { getCurrentInstance, onMounted, reactive, ref, toRefs } from "vue";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();

const loading = ref(true);
const showSearch = ref(true);
const productList = ref([]);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 12,
    name: undefined
  }
});

const { queryParams } = toRefs(data);

/** 查询商品列表 */
function getList() {
  loading.value = true;
  listAllProducts(queryParams.value).then(response => {
    productList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 加入购物车操作 */
function handleAdd(item) {
  const cartForm = {
    productId: item.productId,
    quantity: 1 // 默认每次点击添加1个
  };
  
  addToCart(cartForm).then(response => {
    ElMessage.success("已加入购物车");
    // 刷新列表以更新“已添加数量”显示
    // 优化体验：也可以手动在前端 productList 中找到该 item 并 userCartQuantity++
    getList(); 
  });
}

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.mb20 {
  margin-bottom: 20px;
}
.ml-2 {
  margin-left: 5px;
}

.product-card {
  border: 1px solid #e4e7ed;
  transition: all 0.3s;
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
  }
}

.image-placeholder {
  height: 150px;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #ebeef5;
}

.card-body {
  padding: 15px;
}

.product-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-desc {
  font-size: 13px;
  color: #909399;
  height: 36px; /* 限制两行高度 */
  line-height: 18px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-bottom: 10px;
}

.price-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  
  .price {
    font-size: 18px;
    color: #f56c6c;
    font-weight: bold;
  }
  
  .stock {
    font-size: 12px;
    color: #c0c4cc;
  }
}

.action-row {
  display: flex;
  justify-content: center;
  
  .add-btn {
    width: 100%;
  }
  
  .added-status {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
  }
}
</style>